package com.vegetable.modules.controller.baseData;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.utils.R;
import com.vegetable.modules.entity.baseData.BankCard;
import com.vegetable.modules.entity.baseData.BankCardQueryDTO;
import com.vegetable.modules.service.baseData.IBankCardService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 银行卡号 前端控制器
 * </p>
 *
 * @author vegetable
 * @since 2025-09-23
 */
@Slf4j
@RestController
@RequestMapping("/base-data/bank-card")
@AllArgsConstructor
public class BankCardController {

    private final IBankCardService bankCardService;

    /**
     * 分页查询银行卡号
     */
    @RequestMapping("/list")
//    @RequiresPermissions("base:bankcard:list")
    public R list(@RequestBody BankCardQueryDTO queryDTO) {
        IPage<BankCard> page = bankCardService.queryPage(queryDTO);
        return R.data(page);
    }

    /**
     * 根据ID查询银行卡号
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("base:bankcard:info")
    public R info(@PathVariable("id") Long id) {
        BankCard bankCard = bankCardService.getById(id);
        return R.data(bankCard);
    }

    /**
     * 根据通讯录ID查询银行卡号列表
     */
    @RequestMapping("/list-by-address-book/{addressBookId}")
//    @RequiresPermissions("base:bankcard:list")
    public R listByAddressBookId(@PathVariable("addressBookId") Long addressBookId) {
        List<BankCard> bankCardList = bankCardService.listByAddressBookId(addressBookId);
        return R.data(bankCardList);
    }

    /**
     * 保存银行卡号
     */
    @RequestMapping("/save")
//    @RequiresPermissions("base:bankcard:save")
    public R save(@RequestBody BankCard bankCard) {
        bankCardService.save(bankCard);
        return R.success("保存成功");
    }

    /**
     * 批量保存银行卡号
     */
    @RequestMapping("/save-batch")
//    @RequiresPermissions("base:bankcard:save")
    public R saveBatch(@RequestParam Long addressBookId, @RequestBody List<String> bankNos) {
        boolean result = bankCardService.saveBatch(addressBookId, bankNos);
        if (result) {
            return R.success("保存成功");
        } else {
            return R.fail("保存失败");
        }
    }

    /**
     * 更新银行卡号
     */
    @RequestMapping("/update")
//    @RequiresPermissions("base:bankcard:update")
    public R update(@RequestBody BankCard bankCard) {
        bankCardService.updateById(bankCard);
        return R.success("更新成功");
    }

    /**
     * 设置默认银行卡
     */
    @RequestMapping("/set-default")
//    @RequiresPermissions("base:bankcard:update")
    public R setDefault(@RequestParam Long id, @RequestParam Long addressBookId) {
        boolean result = bankCardService.setDefault(id, addressBookId);
        if (result) {
            return R.success("设置成功");
        } else {
            return R.fail("设置失败");
        }
    }

    /**
     * 删除银行卡号
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("base:bankcard:delete")
    public R delete(@RequestBody Long[] ids) {
        // 软删除
        for (Long id : ids) {
            BankCard card = new BankCard();
            card.setId(id);
            card.setIsDel(1);
            bankCardService.updateById(card);
        }
        return R.success("删除成功");
    }
}